package org.desktop.web.client.custom.widget;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.client.loader.HttpProxy;
import com.sencha.gxt.data.client.writer.UrlEncodingWriter;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.TreeStore;
import com.sencha.gxt.data.shared.loader.ChildTreeStoreBinding;
import com.sencha.gxt.data.shared.loader.JsonReader;
import com.sencha.gxt.data.shared.loader.TreeLoader;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent;
import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent.SelectionChangedHandler;
import com.sencha.gxt.widget.core.client.tree.Tree;

public class AsyncYearTree implements IsWidget {
	
	

	private ContentPanel monthDaysPanel;

	public AsyncYearTree(ContentPanel monthDaysPanel) {
		super();
		this.monthDaysPanel = monthDaysPanel;
	}

	public interface JsonTreeAutoBeanFactory extends AutoBeanFactory {

		AutoBean<RecordResult> items();
	}

	/**
	 * Defines the structure of our JSON records.
	 */
	public interface Record {

		public String getId();

		public String getName();

		public boolean isFolder();

	}

	/**
	 * Defines the structure of the root JSON object being returned by the
	 * server. This class is needed as we cannot return a list of objects.
	 * Instead, we return a single object with a single property that contains
	 * the data records.
	 */
	public interface RecordResult {

		List<Record> getRecords();
	}

	private class RecordKeyProvider implements ModelKeyProvider<Record> {
		@Override
		public String getKey(Record item) {
			return item.getId();
		}
	}

	private class DataRecordJsonReader extends
			JsonReader<List<Record>, RecordResult> {
		public DataRecordJsonReader(AutoBeanFactory factory,
				Class<RecordResult> rootBeanType) {
			super(factory, rootBeanType);
		}

		@Override
		protected List<Record> createReturnData(Object loadConfig,
				RecordResult incomingData) {
			
			return incomingData.getRecords();
		}
	}

	@Override
	public Widget asWidget() {
		JsonTreeAutoBeanFactory factory = GWT
				.create(JsonTreeAutoBeanFactory.class);

		DataRecordJsonReader reader = new DataRecordJsonReader(factory,
				RecordResult.class);

		String url = "http://127.0.0.1:8080/timesheet/json";
		RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + "exec");
//		ScriptTagProxy<Record> jsonProxy = new ScriptTagProxy<AsyncYearTree.Record>(url);
		HttpProxy<Record> jsonProxy = new HttpProxy<Record>(rb);
		jsonProxy
				.setWriter(new UrlEncodingWriter<Record>(factory, Record.class));
		

		TreeLoader<Record> loader = new TreeLoader<Record>(jsonProxy, reader) {
	        @Override
	        public boolean hasChildren(Record parent) {
	          return parent.isFolder();
	        }
	 
	      };		
		VerticalLayoutContainer con = new VerticalLayoutContainer();

		TreeStore<Record> store = new TreeStore<Record>(new RecordKeyProvider());
		loader.addLoadHandler(new ChildTreeStoreBinding<Record>(store));

		final Tree<Record, String> tree = new Tree<Record, String>(store,
				new ValueProvider<Record, String>() {

					@Override
					public String getValue(Record object) {
						return object.getName();
					}

					@Override
					public void setValue(Record object, String value) {
					}

					@Override
					public String getPath() {
						return "name";
					}
				});
		
		final ContentPanel childPanel = this.monthDaysPanel;
		tree.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<AsyncYearTree.Record>() {

			@Override
			public void onSelectionChanged(SelectionChangedEvent<Record> event) {
				// TODO Auto-generated method stub
				Record record = event.getSource().getSelectedItem();
				childPanel.clear();
			}
			
		});
		tree.setLoader(loader);
		con.add(tree,new VerticalLayoutData(1, 1));
		return con;
	}

}
